using Core.SimpleTemp.Common;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Entitys;
using Core.SimpleTemp.Repositories;
using Core.SimpleTemp.Repositories.Sys;
using Core.Toolkit.Common;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Core.Toolkit.Core.Application;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Core.SimpleTemp.Application
{
    [AutoDi()]
    public class PX_PlanReportService : BaseAppService<PX_PlanReportDto, PX_PlanReport, PX_PlanReportRepository>
    {
        readonly ExcelHelp _excelHelp;
        public PX_PlanReportService(PX_PlanReportRepository repository, ExcelHelp excelHelp) : base(repository)
        {
            _excelHelp = excelHelp;
        }



        private LoadPageOffsetModelAuto<PX_PlanReport, PX_PlanReportDto>
            GetLoadPageOffsetMode(PagingModel<PX_PlanReportDto>
                pagingQueryModel)
        {
            var loadPageOffsetModel = this.GetLoadPageOffsetModelAuto(pagingQueryModel);
            var baseQuery = _repository.QueryBase().AsNoTracking();
            loadPageOffsetModel.QueryWhere = this.CreateQRList()

     .Add("PlanId", "=") //
     .Add("Summaryoftraining", "=") //
     .Add("TrainingevaluationmethodsandStrategies", "=") //
     .Add("Trainingcost", "=") //
     .Add("Overallanalysis", "=") //
     .Add("Supportivefactorsandobstacles", "=") //
     .Add("Summaryandrecommendations", "=") //
     .Add("ReportDept", "=") //
     .Add("ReportUser", "=") //
     .Add("ReportTime", "=");//


            loadPageOffsetModel.BasqQuery = baseQuery;
            loadPageOffsetModel.Order = orderModel => orderModel.CreateTime;
            loadPageOffsetModel.Selector = model => new PX_PlanReport()
            {
                Id = model.Id,
                PlanId = model.PlanId,//
                Summaryoftraining = model.Summaryoftraining,//
                TrainingevaluationmethodsandStrategies = model.TrainingevaluationmethodsandStrategies,//
                Trainingcost = model.Trainingcost,//
                Overallanalysis = model.Overallanalysis,//
                Supportivefactorsandobstacles = model.Supportivefactorsandobstacles,//
                Summaryandrecommendations = model.Summaryandrecommendations,//
                ReportDept = model.ReportDept,//
                ReportUser = model.ReportUser,//
                ReportTime = model.ReportTime,//
            };
            return loadPageOffsetModel;
        }


        public async Task<IPageModel<PX_PlanReportDto>> GetList(PagingModel<PX_PlanReportDto>
            pagingQueryModel)
        {
            var loadPageOffsetModel = GetLoadPageOffsetMode(pagingQueryModel);
            return await base.LoadPageOffsetAsync(loadPageOffsetModel);
        }


        public async Task<string>
            Export(PagingModel<PX_PlanReportDto>
                pagingQueryModel)
        {
            var loadPageOffsetModel = GetLoadPageOffsetMode(pagingQueryModel);
            var DataList = await this.ExportDataAsync(loadPageOffsetModel);
            var excelData = DataList.Select(model => new
            {
                model.PlanId,
                PlanId_title = "",
                model.Summaryoftraining,
                Summaryoftraining_title = "",
                model.TrainingevaluationmethodsandStrategies,
                TrainingevaluationmethodsandStrategies_title = "",
                model.Trainingcost,
                Trainingcost_title = "",
                model.Overallanalysis,
                Overallanalysis_title = "",
                model.Supportivefactorsandobstacles,
                Supportivefactorsandobstacles_title = "",
                model.Summaryandrecommendations,
                Summaryandrecommendations_title = "",
                model.ReportDept,
                ReportDept_title = "",
                model.ReportUser,
                ReportUser_title = "",
                model.ReportTime,
                ReportTime_title = "",
            }).ToList();




            return _excelHelp.Create(excelData);
        }

        public async Task<PX_PlanReport> GetAsync(Guid id,string name)
        {

            var userDto = await _repository._dbContext.PX_PlanReport.Where(x => x.PlanId == id).FirstOrDefaultAsync();
            PX_Plan px = await _repository._dbContext.PX_Plan.Where(x => x.Id == id).Include(x=>x.PX_Course).FirstOrDefaultAsync();
            if (userDto == null) {
                userDto = new PX_PlanReport();
                userDto.PlanId = id;
            }
            userDto.Mark01 = px.PX_Course.CoursewareName;
            userDto.Mark02 = name;
            return userDto;
        }

    }

}
